//
//  OneAuthorizeCodeManager.m
//  lTTheOne
//
//  Created by sy on 2025/7/30.
//

#import "OneAuthorizeCodeManager.h"
#import "MSUserManager.h"

@interface OneAuthorizeCodeManager ()
@property(nonatomic,strong,readwrite) OneAuthorizeCodeModel *codeModel;
@end

@implementation OneAuthorizeCodeManager
static OneAuthorizeCodeManager *instance;
static dispatch_once_t onceToken;

+ (instancetype)sharedManager {
    dispatch_once(&onceToken, ^{
      instance = [[self alloc] init];
        [instance instanceAction];
    });
    return instance;
}

- (void)instanceAction{
    NSString*account = [MSUserManager sharedManager].userModel.account;
    NSString *where = [NSString stringWithFormat:@"where %@=%@ ",bg_sqlKey(@"account"),bg_sqlValue(account)];
    
    NSArray *localArr = [OneAuthorizeCodeModel bg_find:AuthorizeCodeTableName where:where];
    if (localArr.count>0) {
        self.codeModel = localArr.firstObject;
    }else{
        self.codeModel = [[OneAuthorizeCodeModel alloc]init];
        self.codeModel.account = [MSUserManager sharedManager].userModel.account;
        
    }
}

- (void)updateCodeAction:(authorizeCode)codeType withCodeStr:(NSString*)codeStr{
    switch (codeType) {
        case chatCode:
            self.codeModel.chat_pwd = codeStr;
            break;
        case momentCode:
            self.codeModel.moment_pwd = codeStr;
            break;
        case homeCode:
            self.codeModel.home_pwd = codeStr;
            break;
        case learningCode:
            self.codeModel.learning_pwd = codeStr;
            break;
        case workingCode:
            self.codeModel.working_pwd = codeStr;
            break;
        case finalStage:
            self.codeModel.fitness_pwd = codeStr;
            break;
        case partyCode:
            self.codeModel.party_pwd = codeStr;
            break;
        case travelCode:
            self.codeModel.travel_pwd = codeStr;
            break;
        default:
            break;
    }
    NSLog(@"更新本地授权码:%ld inputCode:%@",codeType,codeStr);
    self.codeModel.bg_tableName = AuthorizeCodeTableName;
    [self.codeModel bg_saveOrUpdate];
}

- (BOOL)checkLocalCode:(authorizeCode)codeType withNewCode:(NSString*)codeStr{
    NSString *localStr;
    
    switch (codeType) {
        case chatCode:
            localStr = self.codeModel.chat_pwd;
            break;
        case momentCode:
            localStr = self.codeModel.moment_pwd;
            break;
        case homeCode:
            localStr = self.codeModel.home_pwd;
            break;
        case learningCode:
            localStr = self.codeModel.learning_pwd;
            break;
        case workingCode:
            localStr = self.codeModel.working_pwd;
            break;
        case finalStage:
            localStr = self.codeModel.fitness_pwd;
            break;
        case partyCode:
            localStr = self.codeModel.party_pwd;
            break;
        case travelCode:
            localStr = self.codeModel.travel_pwd;
            break;
        default:
            break;
    }
    
    NSLog(@"校验本地授权码:%ld 结果 inputCode:%@ saveCode:%@",codeType,codeStr,localStr);
    return [localStr isEqualToString:codeStr]?YES:NO;
}
@end
